package com.finance.controller;

import com.finance.domain.Money;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

import static com.finance.utils.DateTimeUtil.calculateApartDayConvertString;

@Controller
public class MoneyController extends BaseController {

    /**
     * 跳转user-mgr.html页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/adminMoneyIn")
    public String adminMoneyIn(Model model) {
        return "moneyIn-mgr";
    }

    /**
     * 跳转user-mgr.html页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/adminMoneyOut")
    public String adminMoneOut(Model model) {
        return "moneyOut-mgr";
    }

    /**
     * 通过ajax注册
     *
     * @param money
     * @return
     */
    @RequestMapping("/moneyAdd")
    @ResponseBody
    public Map<String, Object> moneyAdd(Money money) {
        Map<String, Object> map = new HashMap<>();
        money.setCreateBy(String.valueOf(session.getAttribute("USER_ID")));
        int value = moneyService.insertMoney(money);
        if (value == 0) {
            map.put("data", "error");
        } else {
            map.put("data", "success");
        }
        return map;
    }

    @RequestMapping("/deleteMoneyIn/{id}")
    public String deleteMoneyIn(@PathVariable int id) {
        int a = moneyService.deleteMoneyById(id);
        return "redirect:/adminMoneyIn";
    }

    @RequestMapping("/deleteMoneyOut/{id}")
    public String deleteMoneyOut(@PathVariable int id) {
        int a = moneyService.deleteMoneyById(id);
        return "redirect:/adminMoneyOut";
    }

    /**
     * 获取存取款列表
     *
     * @return
     */
    @RequestMapping("/adminGetMoneyInList")
    @ResponseBody
    public Map<String, Object> adminGetMoneyInList() {
        Map<String, Object> map = new HashMap<>();
        Money money = new Money();
        money.setType("存款,取款");
        money.setCreateBy(String.valueOf(session.getAttribute("USER_ID")));
        List<Money> users = moneyService.selectMoneyList(money);
        map.put("data", users);
        return map;
    }

    /**
     * 获取借还款列表
     *
     * @return
     */
    @RequestMapping("/adminGetMoneyOutList")
    @ResponseBody
    public Map<String, Object> adminGetMoneyOutList() {
        Map<String, Object> map = new HashMap<>();
        Money money = new Money();
        money.setType("借钱,还钱");
        money.setCreateBy(String.valueOf(session.getAttribute("USER_ID")));
        List<Money> users = moneyService.selectMoneyList(money);
        map.put("data", users);
        return map;
    }


    @ResponseBody
    @RequestMapping("/getLastWeekendAccess")
    public Map<String, Object> getLastWeekendAccess() {
        Map<String, Object> map = new HashMap<>();

        List<Integer> pv = moneyService.selectSevenDaysPv(String.valueOf(session.getAttribute("USER_ID")));
        List<Integer> uv = moneyService.selectSevenDaysUv(String.valueOf(session.getAttribute("USER_ID")));

        List<Map<String, Object>> l = new ArrayList<>();
        for (int i = 7; i > 0; i--) {
            Map<String, Object> mapLastWeek = new HashMap<>();
            mapLastWeek.put("date", calculateApartDayConvertString(i));
            mapLastWeek.put("pv", pv.get(i - 1));
            mapLastWeek.put("uv", uv.get(i - 1));
            l.add(mapLastWeek);
        }

        map.put("code", 0);
        map.put("info", "success");

        map.put("data", l);

        return map;
    }
}
